//
//  IAAAuthCheckZoomVC.m
//  iPadAuditAuthorise
//
//  Created by XiangDongMBP on 16/10/11.
//  Copyright © 2016年 XiangDong. All rights reserved.
//

#import "IAAAuthCheckZoomVC.h"
#import "IAAAuthCheComCCell.h"
#import "IAAAuthCheDetailCCell.h"
#import "IAAAuthCheckModel.h"

@interface IAAAuthCheckZoomVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UILabel *pageLabel;

@end

static NSString * const reuseCCellId = @"IAAAuthCheDetailCCell";
static NSString * const reuseComCCellId = @"IAAAuthCheComCCell";

@implementation IAAAuthCheckZoomVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initSubViews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initSubViews {    
    UINib *cCellNib = [UINib nibWithNibName:reuseCCellId bundle:nil];
    [self.collectionView registerNib:cCellNib forCellWithReuseIdentifier:reuseCCellId];
    UINib *comCellNib = [UINib nibWithNibName:reuseComCCellId bundle:nil];
    [self.collectionView registerNib:comCellNib forCellWithReuseIdentifier:reuseComCCellId];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    // 滚动到某个位置
//    [self.collectionView reloadData];
    self.pageLabel.text = [NSString stringWithFormat:@"%zd/%zd", self.index + 1, self.model.sortImgList.count];
}

- (void)viewDidLayoutSubviews {
    if (self.index) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.index inSection:0];
        [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
        self.index = 0;
    }
}

#pragma mark - <UICollectionViewDelegateFlowLayout>代理
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return [UIScreen mainScreen].bounds.size;
}

#pragma mark - <UICollectionViewDelegate>代理
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - <UICollectionViewDataSource>代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.model.sortImgList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    IAAAuthCheckSortItem *sortItem = nil;
    if (indexPath.row < self.model.sortImgList.count) {
        sortItem = self.model.sortImgList[indexPath.row];
        if (sortItem.imgArray.count > 1) {
            IAAAuthCheComCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseComCCellId forIndexPath:indexPath];
            [cell configViewWithData:self.model sortItem:sortItem type:IAAAuthCheComCCellTypeAllScreen];
            return cell;
        }
    }
    
    // 单独 or +号
    IAAAuthCheDetailCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseCCellId forIndexPath:indexPath];
    [cell configViewWithData:sortItem type:IAAAuthCheDetailCCellTypeAllScreen];
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5) + 1;
    self.pageLabel.text = [NSString stringWithFormat:@"%zd/%zd", currentPage, self.model.sortImgList.count];
}

@end
